package com.sss.myspringsecurity.config.securityconfig;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sss.myspringsecurity.web.entity.SssMenu;
import com.sss.myspringsecurity.web.entity.SssRoleMenu;
import com.sss.myspringsecurity.web.entity.SssUser;
import com.sss.myspringsecurity.web.entity.SssUserRole;
import com.sss.myspringsecurity.web.service.ISssMenuService;
import com.sss.myspringsecurity.web.service.ISssRoleMenuService;
import com.sss.myspringsecurity.web.service.ISssUserRoleService;
import com.sss.myspringsecurity.web.service.ISssUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 用户信息验证类
 * @author sss
 * @date 2020-02-09 20:10
 **/
@Component
public class CustomerUserDetailService implements UserDetailsService {
    
    @Autowired
    private  ISssUserService sssUserService;
    @Autowired
    private ISssUserRoleService userRoleService;
    @Autowired
    private ISssRoleMenuService roleMenuService;
    @Autowired
    private ISssMenuService menuService;
    
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        SssUser user = sssUserService.getOne(new QueryWrapper<SssUser>().eq("user_name", userName));
        List<GrantedAuthority> list = new ArrayList<>();
        Set<String> menuPermissions = getMenuPermissions(user);
        for (String permission : menuPermissions) {
            //权限集合
            list.add(new SimpleGrantedAuthority(permission));
        }
        CustomerUserDetails userDetails = new CustomerUserDetails();
        userDetails.setSssUser(user);
        userDetails.setAuthorities(list);
        return userDetails;
    }

    /**
     *获得权限字符串
    **/
    private Set<String> getMenuPermissions(SssUser sssUser){
        List<SssUserRole> roleList = userRoleService.list(new QueryWrapper<SssUserRole>().eq("user_id", sssUser.getId()));
        List<Integer> roleIds = roleList.stream().map(SssUserRole::getRoleId).collect(Collectors.toList());
        List<SssRoleMenu> roleMenus = roleMenuService.list(new QueryWrapper<SssRoleMenu>().in("role_id", roleIds));
        List<Integer> collect = roleMenus.stream().map(SssRoleMenu::getMenuId).collect(Collectors.toList());
        List<SssMenu> menus = menuService.list(new QueryWrapper<SssMenu>().in("id", collect));
        Set<String> menusPermissions = menus.stream().map(SssMenu::getPermission).collect(Collectors.toSet());
        return menusPermissions;
    }
}
